#include "customtablemodel.h"
#include<QBrush>

CustomTableModel::CustomTableModel(QObject *parent)
    : QAbstractTableModel(parent){}

int CustomTableModel::rowCount(const QModelIndex &parent) const
{
    //二维数组的行数
    return m_data.size();
}

int CustomTableModel::columnCount(const QModelIndex &parent) const
{
    return m_headers.size();
}

bool CustomTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
    if (role != Qt::EditRole || orientation != Qt::Horizontal)
        return false;

    if (section < 0 || section >= m_headers.size())
        return false;

    m_headers[section] = value.toString();
    emit headerDataChanged(orientation, section, section);
    return true;
}
//role视图想拿什么数据（文字、图标、背景色……）
QVariant CustomTableModel::data(const QModelIndex &index, int role) const
{
    //不合法的判断
    if (!index.isValid() ||
        index.row() >= rowCount() ||
        index.column() >= columnCount())
        return QVariant();
    //拿到存储的元素
    const CustomTableItem &it = m_data.at(index.row()).at(index.column());
    switch (role) {
    case Qt::DisplayRole:
    case Qt::EditRole:
        return it.text;
    case Qt::DecorationRole:
        return it.icon;
    case Qt::ForegroundRole:
        return it.fgColor.isValid() ? QBrush(it.fgColor) : QVariant();
    case Qt::BackgroundRole:
        return it.bgColor.isValid() ? QBrush(it.bgColor) : QVariant();
    }
    return QVariant();
}

QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    //section就是我们拿到第一行每一列的表项
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
        return section < m_headers.size() ? m_headers.at(section) : QVariant();
    return QAbstractTableModel::headerData(section, orientation, role);
}

Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const
{
    if (!index.isValid()) return Qt::NoItemFlags;
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}

bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid()) return false;
    if (role == Qt::EditRole) {
        //向模型中写入数据
        m_data[index.row()][index.column()].text = value.toString();
        //模型数据改变发送信号 更新视图
        emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
        return true;
    }
    return false;
}

//新增一列
void CustomTableModel::appendColumn(const QString &title)
{
    beginInsertColumns(QModelIndex(), columnCount(), columnCount());
    m_headers.append(title);
    for (auto &row : m_data)
        row.append(CustomTableItem());
    endInsertColumns();
}

void CustomTableModel::removeColumn(int column)
{
    if (column < 0 || column >= columnCount()) return;
    beginRemoveColumns(QModelIndex(), column, column);
    m_headers.removeAt(column);
    for (auto &row : m_data)
        row.remove(column);
    endRemoveColumns();
}

void CustomTableModel::setHorizontalHeaderLabels(const QStringList &labels)
{
    if (labels.size() != columnCount()) return;
    m_headers = labels;
    emit headerDataChanged(Qt::Horizontal, 0, columnCount() - 1);
}

void CustomTableModel::appendRow(const QVector<CustomTableItem> &items)
{
    insertRow(rowCount(), items);
}

void CustomTableModel::insertRow(int row, const QVector<CustomTableItem> &items)
{
    if (items.size() != columnCount()) return;
    beginInsertRows(QModelIndex(), row, row);
    m_data.insert(row, items);
    endInsertRows();
}

void CustomTableModel::removeRow(int row)
{
    if (row < 0 || row >= rowCount()) return;
    //先移除视图
    beginRemoveRows(QModelIndex(), row, row);
    //移除模型的数据
    m_data.removeAt(row);
    endRemoveRows();
}

void CustomTableModel::setItem(int row, int column, const CustomTableItem &item)
{
    if (row < 0 || row >= rowCount() || column < 0 || column >= columnCount())
        return;
    m_data[row][column] = item;
    QModelIndex idx = index(row, column);
    emit dataChanged(idx, idx);
}

CustomTableItem CustomTableModel::item(int row, int column) const
{
    //拿的表项数据不是合法的
    if (row < 0 || row >= rowCount() || column < 0 || column >= columnCount())
        return CustomTableItem();
    return m_data.at(row).at(column);
}


